package ink.lovejinhu.leetcode;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author jinhu
 * created 2021-07-13 14:46
 */
public class Solution_0350 {
    /**
     * @param nums1
     * @param nums2
     * @return
     */
    public int[] intersect(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        Arrays.sort(nums2);
        ArrayList<Integer> res = new ArrayList<>();
        for (int i0 = 0, i1 = 0; i0 < nums1.length && i1 < nums2.length; ) {
            if (nums1[i0] == nums2[i1]) {
                res.add(nums1[i0]);
                i0++;
                i1++;
            } else if (nums1[i0] < nums2[i1]) {
                i0++;
            } else {
                i1++;
            }
        }
        int[] ints = new int[res.size()];
        for (int i = 0; i < res.size(); i++) {
            ints[i] = res.get(i);
        }
        return ints;
    }
}
